package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.PersonService;
import com.dk.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("person")
public class PersonController {

    @Reference
    private PersonService personService;

    @Reference
    private RoleService roleService;

    @RequestMapping("show")
    public String show(){
        return "person/show";
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public List<TPerson> getInfo(){
        return personService.getInfo();
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public Boolean deleteInfo(Integer pid){
        Boolean b = personService.deleteInfo(pid);
        return b;
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<TRole> list = roleService.getInfo();
        model.addAttribute("list",list);
        return "person/add";
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public boolean addInfo(TPerson tPerson,Integer[] roleId){
        boolean b = personService.addInfo(tPerson,roleId);
        return b;
    }

    @RequestMapping("toUpdate/{pid}")
    public String toUpdate(Model model, @PathVariable("pid")Integer pid){
        //用户信息
        TPerson TPerson = personService.getInfoById(pid);
        //角色信息
        List<TRole> list = roleService.getInfo();
        //用户对应角色信息
        List<Integer> rids = roleService.getUserRoleById(pid);

        model.addAttribute("person",TPerson);
        model.addAttribute("list",list);
        model.addAttribute("rids",rids);
        return "person/update";
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(TPerson tPerson,@RequestParam("roleId")Integer[] roleId){
        boolean b = personService.updateInfo(tPerson,roleId);
        return b;
    }

    @RequestMapping("login")
    @ResponseBody
    public Map<String,Object> login(TPerson tPerson, HttpServletRequest request, String vcode){
        String s = (String) request.getSession().getAttribute("code");
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("flag",false);
        if(!s.equalsIgnoreCase(vcode)){
            map.put("msg","验证码错误");
            return map;
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(tPerson.getPname(),tPerson.getPwd());
        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            map.put("msg","账户不存在");
            return map;
        }catch (IncorrectCredentialsException e1){
            map.put("msg","密码错误");
            return map;
        }
        map.put("flag",true);
        personService.getByName(tPerson.getPname());
        return map;
    }

}
